/**
 * Copyright (C) <2023> <Boundivore> <boundivore@foxmail.com>
 * <p>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Apache License, Version 2.0
 * as published by the Apache Software Foundation.
 * <p>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Apache License, Version 2.0 for more details.
 * <p>
 * You should have received a copy of the Apache License, Version 2.0
 * along with this program; if not, you can obtain a copy at
 * http://www.apache.org/licenses/LICENSE-2.0.
 */
import { useState, useCallback, useEffect } from 'react';
import { useSearchParams } from 'react-router-dom';
import type { MenuProps } from 'antd';
import RequestHttp from '@/api';
import APIConfig from '@/api/config';
import { NodeDetailVo } from '@/api/interface';
import { useConfigState } from './useConfigState';
import { fuzzySearchFiles } from '@/utils/helper';
// 从 MenuProps 中提取 items 类型
type MenuItem = NonNullable<MenuProps['items']>[number];

// 使用交叉类型将 NodeDetailVo 合并到 MenuItem 中
type MergedTabItem = MenuItem & NodeDetailVo;

// 定义扩展后的 MenuProps 类型
type MergedMenuProps = {
	items: MergedTabItem[];
};
export const useNodeDropdown = (callBack: (nodeId: string) => void) => {
	const [searchParams] = useSearchParams();
	const id = searchParams.get('id');
	const [nodeList, setNodeList] = useState<NodeDetailVo[]>([]);
	const [dropDownItems, setDropDownItems] = useState<MenuProps['items']>([]);
	const [allNodeData, setAllNodeData] = useState<MergedMenuProps['items']>([]);
	const [hostnameArr, setHostnameArr] = useState<string[]>([]);
	const { setCurrentNodeId, setCurrentNodeInfo } = useConfigState();

	const handleSearch = (text: string) => {
		const result = fuzzySearchFiles(hostnameArr, text);
		const filteredTabsData = allNodeData.filter(node => result.includes(node.Hostname));
		setDropDownItems(filteredTabsData);
	};
	// 获取节点列表数据
	const showDownloadList = useCallback(async () => {
		const api = APIConfig.nodeList;
		const params = { ClusterId: id };
		const data = await RequestHttp.get(api, { params });
		const {
			Data: { NodeDetailList }
		} = data;
		setNodeList(NodeDetailList);
	}, [id]);

	// 处理节点选择
	const handleNodeSelect = useCallback(
		(node: NodeDetailVo) => {
			setCurrentNodeId(node.NodeId);
			setCurrentNodeInfo(node);
			callBack?.(node.NodeId);
		},
		// eslint-disable-next-line react-hooks/exhaustive-deps
		[callBack, setCurrentNodeId]
	);

	// 当 nodeList 或 currentNodeId 变化时，更新下拉菜单项
	useEffect(() => {
		const hostnames: string[] = [];
		if (nodeList.length) {
			const transformed = nodeList.map((item: NodeDetailVo) => {
				hostnames.push(item.Hostname);
				return {
					...item,
					key: item.NodeId,
					label: <div onClick={() => handleNodeSelect(item)}>{item.Hostname}</div>
				};
			});
			setDropDownItems(transformed);
			setAllNodeData(transformed);
		}
		setHostnameArr(hostnames);
	}, [nodeList, handleNodeSelect]);

	return {
		dropDownItems,
		showDownloadList,
		handleSearch
	};
};
